package com.wus.common.web.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.wus.common.core.factory.YmlPropertySourceFactory;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

/**
 * @Description: Knife4j配置类
 * @Author: wus
 * @Date: 2024/12/26 10:33
 */
@ConditionalOnProperty(value = "knife4j.enabled", havingValue = "true")
@EnableKnife4j
@PropertySource(value = "classpath:common-knife4j.yaml", factory = YmlPropertySourceFactory.class)
@AutoConfiguration
public class Knife4jConfig {

    /**
     * 创建权限分组api
     *
     */
    /*@Bean
    public GroupedOpenApi securityApi() {
        return createRestApi("权限管理“, "包路径");
    }*/

    /**
     * 创建系统分组api
     *
     */
    @Bean
    public GroupedOpenApi systemApi() {
        return createRestApi("系统管理", "com.wus");
    }

    /**
     * 创建api
     *
     * @param groupName   分组名称
     * @param basePackage 包路径
     * @return GroupedOpenApi
     */
    public GroupedOpenApi createRestApi(String groupName, String basePackage) {
        return GroupedOpenApi.builder()
                .group(groupName) // 分组名称
                .packagesToScan(basePackage) // 扫描的包路径
                .pathsToMatch("/**") // 匹配所有路径
                .addOpenApiCustomizer(openApi -> openApi.info(apiInfo())) // 设置api信息
                .build();
    }

    /**
     * api简介信息
     *
     * @return ApiInfo
     */
    private Info apiInfo() {
        return new Info()
                .title("songCloud系统接口") // 标题
                .description("songCloud系统接口文档...") // 描述
                .version("1.0.0") // 版本号
                .contact(new Contact().name("wus")); // 联系人信息
    }
}
